<?php
class ArticleController extends AppController {
	var $name = 'Article';
	var $components = array('Menu','Init');
	var $uses = array();

	function beforeFilter() {
		$this->Init->startup($this);
		$this->Init->init();
		$this->Init->getKeyword();
		$_session = $this -> Session -> read();
		$this->loadModel('Hitcounter');
		$row = $this->Hitcounter->get(session_id());
		if ($row == 0) {
			$data = array(
				"session_id" => session_id(),
				"ref_url" => env("REQUEST_URI"),
				"ip_address" => env("REMOTE_ADDR"),
				"activity" => date("Y-m-d H:i:s")
			);
			$this->Hitcounter->save($data);
		}
		$_menus = $this->Menu->getMenu($this->params['controller'],$this->params['action']);
		for ($i = 1; $i <=6; $i++) {
			if ($_menus[$i-1] == 1) {
				$this->set('current'.$i,'class="current"');
			}
			else {
				$this->set('current'.$i,'');
			}
		}
	}

	function promotion() {
		$this->layout = 'inside';
		$this->loadModel('News');

		if (!$this->Session->read("lang")) {
			$lang = "vie";
		} else {
			$lang = $this->Session->read("lang");
		}

		Configure::write("Config.language", $lang);

		$this->paginate = array(
			'News' => array (
				'limit'      => 10,
				'conditions' =>  array('News.status' => 1, 'News.kind' => 0),
				'order'		 =>  'News.date desc',
				'fields' 	 =>  array('News.title_'.$lang,'News.description_'.$lang,'News.image','News.id','News.tag')
			)
		);
		$this->set('lang',$lang);
		$this->set('articles', $this->paginate('News'));
	}

	function project() {
		$this->layout = 'inside';
		$this->loadModel('Picture');

		if (!$this->Session->read("lang")) {
			$lang = "vie";
		} else {
			$lang = $this->Session->read("lang");
		}

		Configure::write("Config.language", $lang);

		$this->paginate = array(
			'Picture' => array (
				'limit'         => 30,
				'order' 		=> "{$this->Picture->alias}.date desc",
				'conditions'	=> array('Picture.status' => 1, 'Picture.kind' => 5),
				'fields' 	    =>  array('Picture.name_'.$lang,'Picture.image_link','Picture.id')
			)
		);
		$this->set('lang',$lang);
		$this->set('projects', $this->paginate('Picture'));
	}

	function detail($id = null) {
		$this->layout = 'inside';
		$id = intval($id);
		if($id < 1) {
			$this->redirect(array('action'=>'index'));
		}

		/***/
		if (!$this->Session->read("lang")) {
			$lang = "vie";
		} else {
			$lang = $this->Session->read("lang");
		}

		Configure::write("Config.language", $lang);
		/***/

		$this->loadModel('News');

		$this->set('news', $this->News->find('first', array(
			'conditions' => array(
				'News.id'  => $id
			),
			'order'	=> 'News.date desc',
			'fields' => array('News.id','News.title_'.$lang, 'News.content_'.$lang,'News.tag', 'News.date')
		)));

		// other news
		$other_news = $this->News->find('all', array(
			'conditions' => array(
				'News.id !='  => $id,
				'News.status' => 1
			),
			'limit'	=> 5,
			'order'	=> 'News.date desc',
			'fields' => array('News.id','News.title_'.$lang, 'News.content_'.$lang)
		));
		$this->set('lang', $lang);
		$this->set('other_news', $other_news);
	}
}
?>
